package handler

import (
	"context"
	"fmt"
	"go.uber.org/zap"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"shop/goods-srv/global"
	"shop/goods-srv/model"
	bannerproto "shop/goods-srv/proto"
)

type BannerService struct {
}

func ModelToResponse(banner model.Banner) bannerproto.BannerResponse {
	bannerResp := bannerproto.BannerResponse{
		BannerId:    banner.ID,
		BannerImage: banner.Image,
		BannerUrl:   banner.Url,
		BannerIndex: banner.Index,
	}
	return bannerResp
}

// banner列表
func (b *BannerService) GetBannerList(ctx context.Context, req *bannerproto.PageInfo) (*bannerproto.BannerListResponse, error) {
	var banners []model.Banner
	pn := int(req.Pn)
	psize := int(req.Psize)
	result := global.Db.Scopes(Paginate(pn, psize)).Find(&banners)
	if result.Error != nil {
		zap.S().Fatalf("banner列表数据获取失败%s", result.Error)
	}
	resp := &bannerproto.BannerListResponse{}
	var total int64
	global.Db.Model(&model.Banner{}).Count(&total)
	resp.Total = int32(total)

	for _, banner := range banners {
		bannerInfoResponse := ModelToResponse(banner)
		resp.Data = append(resp.Data, &bannerInfoResponse)
	}
	return resp, nil
}

func (b *BannerService) CreateBanner(ctx context.Context, req *bannerproto.CreateBannerRequest) (*bannerproto.CreateBannerResponse, error) {
	var banner model.Banner

	if res := global.Db.First(&banner); res.RowsAffected != 0 {
		return nil, status.Errorf(codes.AlreadyExists, "Banner already exists")
	}
	banner.Image = req.BannerImage
	banner.Url = req.BannerUrl
	banner.Index = req.BannerIndex
	res := global.Db.Create(&banner)
	if res.RowsAffected == 0 {
		zap.S().Fatal("banner创建失败")
	}
	resp := &bannerproto.CreateBannerResponse{
		BannerId: banner.ID,
	}
	return resp, nil
}
func (b *BannerService) UpdateBanner(ctx context.Context, req *bannerproto.UpdateBannerRequest) (*emptypb.Empty, error) {
	var banner model.Banner
	result := global.Db.First(&banner, req.BannerId)
	fmt.Println(result, "result")
	if result.RowsAffected == 0 {
		zap.S().Fatal("banner详情不存在")
	}

	if req.BannerImage != "" {
		banner.Image = req.BannerImage
	}
	if req.BannerUrl != "" {
		banner.Url = req.BannerUrl
	}
	if req.BannerIndex != 0 {
		banner.Index = req.BannerIndex
	}

	global.Db.Save(&banner)

	return &emptypb.Empty{}, nil
}

// 删除
func (b *BannerService) DeleteBanner(ctx context.Context, req *bannerproto.DeleteBannerRequest) (*emptypb.Empty, error) {
	var banner model.Banner
	result := global.Db.Delete(&banner, req.BannerId)
	fmt.Println(result, "result")
	if result.Error != nil {
		zap.S().Fatal("banner删除失败")
	}
	return &emptypb.Empty{}, nil
}

// 获取banner详情
func (b *BannerService) GetBannerInfo(ctx context.Context, req *bannerproto.GetBannerInfoRequest) (*bannerproto.BannerResponse, error) {
	var banner model.Banner

	result := global.Db.First(&banner, req.Id)
	if result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "banner不存在")
	}
	if result.Error != nil {
		return nil, result.Error
	}

	resp := ModelToResponse(banner)

	return &resp, nil
}
